<template>
  <div class="search-result-container">
    <!-- 点击实现后退效果 -->
    <van-nav-bar
      title="搜索结果"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
    />
    <van-list
      offset="50"
      :immediate-check=false
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ArticleItem
        v-for="item in jieguoliebiao"
        :key="item.art_id"
        :item="item"
        :chazishow="false"
      ></ArticleItem>
    </van-list>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem'
import { searchResultAPI } from '@/api'
import { gettime } from '@/utils/date'
export default {
  name: 'SearchResult',
  components: {
    ArticleItem
  },
  data () {
    return {
      jieguoliebiao: [], // 文章结果列表
      loading: false,
      finished: false,
      page: 1// 页码
    }
  },
  created () {
    this.qingqiuList()
  },

  methods: {
    //   把请求数据的步骤封装起来
    async qingqiuList () {
      const res = await searchResultAPI({
        kw: this.$route.params.kw,
        page: this.page
      })
      console.log(res)
      if (res.data.data.results.length === 0) {
        this.finished = true
        return
      }
      // 时间的预处理
      res.data.data.results.forEach(item => {
        item.pubdate = gettime(item.pubdate) // 也可以引入直接使，就不用this.gettime了
      })
      this.jieguoliebiao = [...this.jieguoliebiao, ...res.data.data.results]
      this.loading = false
    },
    // 上拉加载的函数
    onLoad () {
      this.page++
      this.qingqiuList()
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
